<template>
	<view class="content">
		<view class="box">
			<view class="item" v-for="item in addressList">
				<view class="top" @click="openEdit(item)">
					<view class="name">
						<view class="left">
							{{item.name}}
						</view>
						<view class="right">
							{{item.mobile}}
						</view>
					</view>
					<view class="address">
						{{item.address}}
					</view>
				</view>
				<view class="button">
					<view class="checkbox">
						<checkbox-group @change="changeCheckbox(item)">
							<label>
								<checkbox :value="item.address_id" :checked="item.is_default == 1" color="#FFCC33"
									style="transform:scale(0.7)" />
								<text>设为默认收货地址</text>
							</label>
						</checkbox-group>
					</view>
					<view class="del" @click="delAddress(item.address_id)">
						<u-icon name="trash"></u-icon>
						<text>删除</text>
					</view>
				</view>
			</view>
		</view>
		<view class="add" @click="open">
			+ 添加收货地址
		</view>
		<u-modal :show="show" content="确认删除？" showCancelButton @confirm="delAddressSubmit"
			@cancel="delAddressClose"></u-modal>
		<uni-popup ref="popup" type="center" :mask-click="false">
			<view class="addresspopup">
				<view class="close" @click="close">
					<u-icon name="close" size="30" color="#fff" bold></u-icon>
				</view>
				<view class="address-title">
					添加收货地址
				</view>
				<view class="address-item">
					<view class="label">
						收货人
					</view>
					<view class="cox">
						<input type="text" placeholder="收货人姓名" placeholder-class="placeholder" v-model="addForm.name" />
					</view>
				</view>
				<view class="address-item">
					<view class="label">
						手机号
					</view>
					<view class="cox">
						<input type="text" placeholder="收货人手机号" placeholder-class="placeholder" maxlength="11"
							v-model="addForm.mobile" @input="inputNumber" />
					</view>
				</view>
				<view class="address-item">
					<view class="label">
						地点
					</view>
					<view class="cox selectcox" @click="$refs.scroll.open()">
						<view class="address-cox">
							<view style="width: 100%;">{{addForm.address || '选择收货地址'}}</view>
						</view>
						<u-icon name="arrow-right" size="20"></u-icon>
					</view>
				</view>
				<view class="address-item">
					<view class="label">
						详细地址
					</view>
					<view class="cox selectcox">
						<input type="text" placeholder="如街道、小区、乡镇等" placeholder-class="placeholder"
							v-model="addForm.detail_address" />
					</view>
				</view>
				<view class="save-btn">
					<view class="save" @click="saveAddress(addForm,0)">
						保存
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popupEdit" type="center" :mask-click="false">
			<view class="addresspopup">
				<view class="close" @click="closeEdit">
					<u-icon name="close" size="30" color="#fff" bold></u-icon>
				</view>
				<view class="address-title">
					编辑收货地址
				</view>
				<view class="address-item">
					<view class="label">
						收货人
					</view>
					<view class="cox">
						<input type="text" placeholder="收货人姓名" placeholder-class="placeholder" v-model="editForm.name" />
					</view>
				</view>
				<view class="address-item">
					<view class="label">
						手机号
					</view>
					<view class="cox">
						<input type="text" placeholder="收货人手机号" placeholder-class="placeholder" maxlength="11"
							v-model="editForm.mobile" @input="inputNumber" />
					</view>
				</view>
				<view class="address-item">
					<view class="label">
						地点
					</view>
					<view class="cox selectcox" @click="$refs.scroll.open()">
						<view class="address-cox">
							<view style="width: 100%;">{{editForm.address || '选择收货地址'}}</view>
						</view>
						<u-icon name="arrow-right" size="20"></u-icon>
					</view>
				</view>
				<view class="address-item">
					<view class="label">
						详细地址
					</view>
					<view class="cox selectcox">
						<input type="text" placeholder="如街道、小区、乡镇等" placeholder-class="placeholder"
							v-model="editForm.detail_address" />
					</view>
				</view>
				<view class="save-btn">
					<view class="save" @click="saveAddress(editForm,editForm.is_default)">
						保存
					</view>
				</view>
			</view>
		</uni-popup>
		<liu-choose-address ref="scroll" @change='changeAddressPicker'></liu-choose-address>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				addressList: [],
				paging: {
					page: 1,
					limit: 10,
				},
				total: null,
				id: '',
				addForm: {
					name: '',
					mobile: '',
					address: '',
					detail_address: ''
				},
				editForm: {},
				type: ''
			}
		},
		onLoad() {
			this.getAddressList()
		},
		onReachBottom() {
			this.getAddressList()
		},
		methods: {
			getAddressList() {
				if (this.total == this.addressList.length) return
				this.$api.addresslist(this.paging).then(res => {
					console.log(res);
					this.addressList = [...this.addressList, ...res.data.list]
					this.total = res.data.total
					this.paging.page++
				})
			},
			delAddressSubmit() {
				this.$api.deladdress({
					id: this.id
				}).then(res => {
					this.delAddressClose()
					this.updataList()
				})
			},
			delAddressClose() {
				this.show = false
				this.id = ''
			},
			delAddress(id) {
				this.show = true
				this.id = id
			},
			open() {
				this.$refs.popup.open()
				this.type = 'add'
			},
			openEdit(obj) {
				this.$refs.popupEdit.open()
				this.editForm = obj
				this.type = 'edit'
			},
			close() {
				this.$refs.popup.close()
				setTimeout(() => {
					this.addForm.name = ''
					this.addForm.mobile = ''
					this.addForm.address = ''
					this.addForm.detail_address = ''
				}, 200)
			},
			closeEdit() {
				this.$refs.popupEdit.close()
				setTimeout(() => {
					this.editForm = {}
				}, 200)
			},
			inputNumber() {
				setTimeout(() => {
					this.addForm.mobile = this.addForm.mobile.replace(/[^\d.]/g, "")
				}, 10)
			},
			changeAddressPicker(e) {
				console.log(e);
				if (this.type == 'edit') {
					this.editForm.address = e.value.map(item => item.label).join('')
				} else {
					this.addForm.address = e.value.map(item => item.label).join('')
				}
			},
			saveAddress(form, is_default) {
				this.$api.addresssave({
					...form,
					is_default
				}).then(res => {
					if (this.type == 'edit') {
						this.closeEdit()
					} else {
						this.close()
					}
					this.type = ''
					this.updataList()
				})
			},
			changeCheckbox(obj) {
				let is_default = null
				if (obj.is_default == 1) is_default = 0
				if (obj.is_default == 0) is_default = 1
				this.saveAddress(obj, is_default)
			},
			updataList(){
				this.paging.page = 1
				this.total = null
				this.addressList = []
				this.getAddressList()
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		min-height: 100vh;
		z-index: -1;
		background: #F5F5F5;
	}

	.box {
		padding: 0 30rpx;
		padding-top: 10rpx;
		padding-bottom: 200rpx;
	}

	.item {
		height: 250rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		box-sizing: border-box;
		padding: 0 30rpx;
		margin-bottom: 16rpx;

		.top {
			padding: 48rpx 0 30rpx;
			border-bottom: 2rpx solid #d4d4d4;

			.name {
				display: flex;

				.left {
					font-family: PingFangSC, PingFang SC;
					font-weight: 500;
					font-size: 32rpx;
					color: #111111;
					line-height: 44rpx;
					text-align: left;
					font-style: normal;
				}

				.right {
					padding-left: 16rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
					line-height: 44rpx;
					text-align: left;
					font-style: normal;
				}
			}

			.address {
				padding-top: 10rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #999999;
				line-height: 34rpx;
				text-align: left;
				font-style: normal;
			}
		}

		.button {
			display: flex;
			justify-content: space-between;
			padding-top: 22rpx;

			.checkbox {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 24rpx;
				color: #333333;
				line-height: 34rpx;
				text-align: left;
				font-style: normal;
			}

			.del {
				display: flex;

				text {
					padding-left: 5rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 500;
					font-size: 24rpx;
					color: #333333;
					line-height: 45rpx;
					text-align: left;
					font-style: normal;
				}
			}
		}
	}

	.add {
		position: fixed;
		bottom: 60rpx;
		left: 50%;
		transform: translate(-50%);
		width: 630rpx;
		height: 84rpx;
		background: linear-gradient(270deg, #EA6F29 0%, #FAAA7D 100%);
		border-radius: 42rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		font-size: 30rpx;
		color: #FFFFFF;
		line-height: 84rpx;
		text-align: center;
		font-style: normal;
	}

	.addresspopup {
		width: 570rpx;
		height: 760rpx;
		background: #FFFFFF;
		border-radius: 40rpx;
		box-sizing: border-box;
		position: relative;

		.close {
			width: 90rpx;
			height: 90rpx;
			border-radius: 50%;
			border: 8rpx solid white;
			position: absolute;
			left: 50%;
			bottom: -150rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			transform: translateX(-50%);
		}

		.address-title {
			padding-top: 70rpx;
			padding-bottom: 30rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 32rpx;
			color: #111111;
			line-height: 44rpx;
			text-align: center;
			font-style: normal;
		}

		.address-item {
			padding: 30rpx 30rpx;
			box-sizing: border-box;
			border-bottom: 2rpx solid #d6d6d6;
			display: flex;
			align-items: center;

			.label {
				width: 120rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #111111;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
			}

			.cox {
				flex: 1;

				/deep/.placeholder {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					line-height: 40rpx;
					text-align: left;
					font-style: normal;
				}
			}

			.selectcox {
				display: flex;
				justify-content: space-between;

				.address-cox {
					flex: 1;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #111111;
					line-height: 40rpx;
					text-align: left;
					font-style: normal;
				}
			}
		}

		.save-btn {
			padding-top: 60rpx;
			display: flex;
			justify-content: center;

			.save {
				width: 470rpx;
				height: 84rpx;
				background: linear-gradient(270deg, #EA6F29 0%, #FAAA7D 100%);
				border-radius: 42rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 84rpx;
				text-align: center;
				font-style: normal;
			}
		}
	}
</style>